package andriod.action.info;

import andriod.service.ArdInfoService;

import com.jcrj.action.BaseAction;
import com.jcrj.entity.OFile;
import com.jcrj.entity.OInfo;
import com.jcrj.entity.OUser;
import com.jcrj.service.FileService;
import com.jcrj.service.InfoManagerService;
import com.jcrj.service.SysLogService;
import com.jcrj.util.DateProcessor;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

public class ArdInfoAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private ArdInfoService ardInfoService;
	private InfoManagerService infoManagerService;
	private FileService fileService;
	private int infoId;
	private int docId;
	private String[] orgIds;
	private String name;
	private String columnId;
	private String title;
	private String startDate;
	private String endDate;
	private long totalCount;
	private long pageCount;

	public String infoHome() {
		try {
			JSONObject obj = new JSONObject();
			long count = this.ardInfoService.countInfo();
			obj.put("ggCount", Long.valueOf(count));
			OUser user = (OUser) super.getSession().get("userInfo");
			String orgIds = this.infoManagerService.getUserAllOrgs(user
					.getOrgId().intValue());
			boolean role = this.infoManagerService.getRoleByUser(user.getId()
					.intValue());
			count = this.ardInfoService.countNotice(orgIds, role);
			obj.put("tbCount", Long.valueOf(count));
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public void setColumnId(String columnId) {
		this.columnId = columnId;
	}

	public String getInfoList() {
		try {
			this.totalCount = this.infoManagerService.countTotalOInfos("1",
					super.getTitle(), super.getStartDate(), super.getEndDate());
			this.pageCount = ((this.totalCount - 1L) / 10L + 1L);
			// if (super.getCurrentPage() > this.pageCount) {
			// super.setCurrentPage(Long.valueOf(this.pageCount).intValue());
			// }

			List<?> list = this.infoManagerService.getOInfos(this.columnId,
					super.getTitle(), super.getStartDate(), super.getEndDate(),
					super.getCurrentPage());
			JsonConfig config = new JsonConfig();
			config.registerJsonValueProcessor(Date.class, new DateProcessor());
			JSONObject obj = new JSONObject();
			JSONArray array = JSONArray.fromObject(list, config);

			obj.put("totalCount", this.totalCount);
			obj.put("pageCount", this.pageCount);
			obj.put("currentPage", super.getCurrentPage());
			obj.put("list", array);

			super.getResponse().setCharacterEncoding("UTF-8");
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();

		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String getNoticeList() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			String orgs = this.infoManagerService.getUserAllOrgs(user
					.getOrgId().intValue());
			boolean role = this.infoManagerService.getRoleByUser(user.getId()
					.intValue());
			long totalCount = this.infoManagerService.countNoticeAll(orgs,
					this.name, super.getStartDate(), super.getEndDate(),
					super.getTitle(), role);
			long pageCount = ((totalCount - 1L) / 10L + 1L);
			if (super.getCurrentPage() > pageCount) {
				super.setCurrentPage(Long.valueOf(pageCount).intValue());
			}
			List noticeList = this.infoManagerService.findNoticeList(
					super.getCurrentPage(), orgs, this.name,
					super.getStartDate(), super.getEndDate(), super.getTitle(),
					role);

			JsonConfig config = new JsonConfig();
			config.registerJsonValueProcessor(Date.class, new DateProcessor());
			config.registerJsonValueProcessor(java.util.Date.class,
					new DateProcessor());
			JSONArray array = JSONArray.fromObject(noticeList, config);
			JSONObject obj = new JSONObject();
			obj.put("totalCount", totalCount);
			obj.put("currentPage", super.getCurrentPage());
			obj.put("pageCount", pageCount);
			obj.put("list", array);
			HttpServletResponse response = super.getResponse();
			response.setContentType("text/json;charset=utf-8");
			PrintWriter out = super.getResponse().getWriter();
			out.print(obj.toString());
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String getInfoView() {
		try {
			OInfo info = (OInfo) this.infoManagerService.getObject(new OInfo(),
					this.infoId);
			List<OFile> list = this.fileService.getFileList(info.getFileId());
			JSONObject obj = new JSONObject();
			obj.put("info", info);
			obj.put("content",
					this.ardInfoService.getBLOBString(info.getContentId()));
			obj.put("fileList", list);
			super.getResponse().setCharacterEncoding("UTF-8");
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public String saveNoticeInfo() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			this.infoManagerService.saveNotice(this.docId, this.orgIds, user);
			JSONObject obj = new JSONObject();
			obj.put("flag", Boolean.valueOf(true));
			super.getResponse().getWriter().write(obj.toString());
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public ArdInfoService getArdInfoService() {
		return this.ardInfoService;
	}

	public void setArdInfoService(ArdInfoService ardInfoService) {
		this.ardInfoService = ardInfoService;
	}

	public InfoManagerService getInfoManagerService() {
		return this.infoManagerService;
	}

	public void setInfoManagerService(InfoManagerService infoManagerService) {
		this.infoManagerService = infoManagerService;
	}

	public FileService getFileService() {
		return this.fileService;
	}

	public void setFileService(FileService fileService) {
		this.fileService = fileService;
	}

	public int getInfoId() {
		return this.infoId;
	}

	public void setInfoId(int infoId) {
		this.infoId = infoId;
	}

	public int getDocId() {
		return this.docId;
	}

	public void setDocId(int docId) {
		this.docId = docId;
	}

	public String[] getOrgIds() {
		return this.orgIds;
	}

	public void setOrgIds(String[] orgIds) {
		this.orgIds = orgIds;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	private String[] ids;

	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}

	/**
	 * 删除最新公告
	 * 
	 * @return
	 */
	public String informationReviewDeleteInformation() {
		try {
			this.infoManagerService.deleteInfos(this.ids);
			super.getResponse().getWriter().write("{\"flag\":true}");
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	private SysLogService sysLogService;

	public SysLogService getSysLogService() {
		return sysLogService;
	}

	public void setSysLogService(SysLogService sysLogService) {
		this.sysLogService = sysLogService;
	}

	private String[] orgs;

	public String[] getOrgs() {
		return orgs;
	}

	public void setOrgs(String[] orgs) {
		this.orgs = orgs;
	}

	public String deleteNotice() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			this.infoManagerService.deleteNotice(this.orgs);
			this.sysLogService.saveLog("信息管理", user.getName(), "删除了通知通报信息");
			super.getResponse().getWriter().write("{\"flag\":true}");
			super.getResponse().getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				super.getResponse().getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}